package com.apes.fn.scm.vipSettle.repository;

import com.apes.fn.scm.retailOrder.model.RetailOrder;
import com.apes.fn.scm.vipSettle.model.VipUnionSettle;
import com.apes.framework.jpa.repository.CustomRepository;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface VipUnionSettleRepository extends CustomRepository<VipUnionSettle, String> {

    @Query("SELECT COUNT(T) AS CZBJ FROM VipUnionSettle T  WHERE T.retailOrder = :retailOrderId " +
            "AND T.company.id = :companyId AND T.partnerCompany.id = :partnerCompanyId AND T.type = :type AND T.paymentMethodType = :paymentMethodType")
    int existsVipSettle(@Param("retailOrderId") String retailOrderId, @Param("companyId") String companyId,
                        @Param("partnerCompanyId") String partnerCompanyId, @Param("type") String type,@Param("paymentMethodType") String paymentMethodType);

    @Query("SELECT T FROM VipUnionSettle T,VipUnionSettleItem B  WHERE T.id = B.vipUnionSettle.id " +
            "AND T.retailOrder = :retailOrderId " +
            "AND T.company.id = :companyId AND T.partnerCompany.id = :partnerCompanyId " +
            "AND B.vipBalanceType = :vipBalanceType " +
            "AND T.type = :type AND T.paymentMethodType = 'discount'" +
            "AND T.dept.id = :deptId AND T.partnerDept.id = :partnerDeptId")
    VipUnionSettle existsVipSettleDiscount(@Param("retailOrderId") String retailOrderId, @Param("companyId") String companyId,@Param("deptId") String deptId,
                                           @Param("partnerDeptId") String partnerDeptId,@Param("partnerCompanyId") String partnerCompanyId, @Param("vipBalanceType") int vipBalanceType,
                                           @Param("type") String type);


    @Query("SELECT A.operation FROM Corporate A ,Party B,PartyRole C WHERE B.id = C.party.id AND C.id = :id AND B.corporate.id = A.id")
    Enumeration findOperation(@Param("id") String company_id);

    @Query("SELECT COUNT(T) FROM VipUnionSettle T WHERE T.retailOrder = :retailOrderId")
    int findByRetailOrder(@Param("retailOrderId") String retailOrderId);

    @Query("SELECT T FROM VipUnionSettle T WHERE T.retailOrder = :retailOrderId AND T.type = 'in' AND T.paymentMethodType = 'discount'")
    List<VipUnionSettle> findByType(@Param("retailOrderId") String retailOrderId);

}
